package com.jung.administrator.service.impl;

import javax.annotation.Resource;

import com.jung.administrator.dao.AdministratorDao;
import com.jung.administrator.model.Administrator;
import com.jung.administrator.service.AdministratorService;

public class AdministratorServiceImpl implements AdministratorService {

	private AdministratorDao administratorDao;
	
	@Resource
	public void setAdministratorDao(AdministratorDao administratorDao) {
		this.administratorDao = administratorDao;
	}

	public boolean addAdministrator(Administrator administrator) {
		// TODO Auto-generated method stub
		
          return administratorDao.add(administrator);
	}

	public boolean updatePassword(String userName, String password) {
		Administrator administrator = getAdministratorByUserName(userName);
		administrator.setPassword(password);
		return administratorDao.updateAdmin(administrator);
	}
     public Administrator getAdministratorByUserName(String userName){
    	 return administratorDao.getByUserName(userName);
     }

	public String login(String userName, String password) {
		Administrator administrator = getAdministratorByUserName(userName);
		if(administrator==null){
			return "userError";
		}
		if(administrator.getPassword().equals(password))
		{
			return "match";
		}
		return "passError";
	}
	public String updateAccount(String  preUserName, String prePassword,String newUserName,String newPassword) {
		
		Administrator administrator = getAdministratorByUserName(preUserName);
		if(administrator==null){
			return "none";
		}
		if(administrator.getPassword().equals(prePassword))
		{
			administrator.setUsername(newUserName);
			administrator.setPassword(newPassword);
			administratorDao.updateAdmin(administrator);
			return "success";
		}
		return "failure";
	}
     
}
